<?php
class IndexController extends Zendvn_Controller_Action{
	private $_paning = array('rowCount' => 40, 'pageRange' => 5);
	public function init(){
		parent::init();		
		$this->_templateObj->setPath(TEMPLATE_PATH . '/public/default');		
		$this->_templateObj->load();	
		
		/*====================================================
		 * Load Zendvn Translate for Controller
		 *====================================================*/
		$this->_langObj->setLangFile(array('validate.tmx'));		
		Zend_Registry::set('Zend_Translate',$this->_langObj->generate());
		
		$this->view->addScriptPath(TEMPLATE_PATH . '/public/default');
		
		/*====================================================
		 * Thiet lap gia tri cho phan phan trang cua controlle
		 *====================================================*/
		$pageNumber = $this->_request->getParam('page',1);
		$this->_paning['page'] = $pageNumber;
		$this->_arrParam['panigator'] = $this->_paning;
		$this->view->arrParam = $this->_arrParam;
	}
	
	public function indexAction(){
        $this->view->headLink()->appendStylesheet(PUBLIC_URL.'/colorpicker/css/colorpicker.css','screen' );
        $this->view->headScript()->appendFile(PUBLIC_URL.'/colorpicker/js/colorpicker.js');

		$tblItem = new Zendvn_Model_Table_NewsItem();
		$this->view->Items = $tblItem->listItem($this->_arrParam, array('task' => 'list-item'));

		$totalItem = $tblItem->totalItem($this->_arrParam);
		$panigator = Zendvn_Paginator::create($this->_arrParam['panigator'],$totalItem);
		$this->view->panigator = $panigator;
	}
	
	public function ajaxLoginAction(){
    	if($this->_request->isPost()){
			$validator = new Default_Form_ValidateLogin($this->_arrParam);
			if($validator->isVaild()){
				$arrError 	= $validator->getMessageErrors();
				$response 	= array('type' => 'error','msg'=>$arrError);
			}else{
				$auth = new Zendvn_System_Auth();
		
				if($auth->login($this->_arrParam) == true){
					$auth = Zend_Auth::getInstance();
					$info = new Zendvn_System_Info();
					$info->setInfo($auth);
				}
				$response 	= array('type' => 'success');
			}
			echo json_encode($response);
			$this->_helper->viewRenderer->setNoRender();
		}
		$this->_helper->layout()->disableLayout();
    }
	
	public function userRegisterAction(){
		$tblItem = new Zendvn_Model_Table_User();
		if($this->_request->isPost()){
            $validator = new Default_Form_ValidateRegister($this->_arrParam);
            if($validator->isVaild()){
                $arrError = $validator->getMessageErrors();
                $this->view->Item 	= $validator->getData();
                $response = array('type' => 'error','msg'=>$arrError);
            }else{

                $this->_helper->flashMessenger->addMessage(array('success'=>'The updated was successful'));
                $data =  $validator->getData();

				$birthday = empty($data['birthday'])?"now":$data['birthday'];
                $data['birthday'] = date("Y-m-d", strtotime($birthday));
                $itemID = $tblItem->saveItem($data,array('task'=>'register'));           
                $mail = new Zendvn_Mail();
                $mail->sendRegister($data);


                $auth = new Zendvn_System_Auth();

                if($auth->login($this->_arrParam) == true){
                    $auth = Zend_Auth::getInstance();
                    $info = new Zendvn_System_Info();
                    $info->setInfo($auth);
                }
                $response = array('type' => 'success');
            }
            echo json_encode($response);
            $this->_helper->viewRenderer->setNoRender();
        }
		$this->_helper->layout->disableLayout();
	}
	
	public function browseAction(){
		if(!empty($this->_arrParam['alias'])){
			$tblItem = new Zendvn_Model_Table_PageContent();
			$this->view->Item = $tblItem->getItem(array('page' => $this->_arrParam['alias']), array('task' => 'base-alias'));
	
			/*========================================
			 * Set meta title - keywords - description
			*========================================*/
			$title = Zendvn_Filter_Filter::filterContent($this->view->Item['name'], 'filter');
			$option['keywords'] = Zendvn_Filter_Filter::filterContent($this->view->Item['html_keyword'], 'filter');
			
			$html_description 	= Zendvn_Filter_Filter::filterContent($this->view->Item['html_descript'], 'filter');
			$article_content 	= Zendvn_Filter_Filter::filterContent(trim(strip_tags($this->view->Item['content'])), 'filter');
			$article_content 	= Zendvn_Filter_Filter::shortString($article_content, 305);
			$description		= empty($html_description)?$article_content:$html_description;
			$option['description'] = $description;
			
			$this->setTitle($title, $option);
		}
	}

    public function addNewItemAction(){
        $tblCat = new Zendvn_Model_Table_NewsCategory();
        $tblItem = new Zendvn_Model_Table_NewsItem();
        $tblQueue= new Zendvn_Model_Table_QueueItem();

        $this->view->slbCategory = $tblCat->itemInSelect($this->_arrParam, array('task'=>'item-add'));
        if($this->_request->isPost()){
            $validator = new Admin_Form_ValidateNewsItem($this->_arrParam);
            if($validator->isVaild()){
                $arrError = $validator->getMessageErrors();
                $this->view->Item 	= $validator->getData();
                $response = array('type' => 'error','msg'=>$arrError);
            }else{

                $this->_helper->flashMessenger->addMessage(array('success'=>'The updated was successful'));
                $data =  $validator->getData();

                $data['status'] = 'pending';
                $itemID = $tblItem->saveItem($data,array('task'=>'admin-add'));
                $tblQueue->addItem(array('id_item'=>$itemID));
                
                $response = array('type' => 'success');
            }
            echo json_encode($response);
            $this->_helper->viewRenderer->setNoRender();
        }
        $this->_helper->layout->disableLayout();
    }

    public function pageContentAction(){
        $tblItem = new Zendvn_Model_Table_PageContent();
        $this->view->Items = $tblItem->listItem(array(),array('task'=>'front-end'));
    }
    
    public function tabsPanelAction(){
    	$tblItem = new Zendvn_Model_Table_PageContent();
    	$this->view->faq = $tblItem->getItem(array('page' => 'faq'), array('task' => 'base-alias'));
    	$this->view->donate = $tblItem->getItem(array('page' => 'donate'), array('task' => 'base-alias'));
    	$this->view->privacy = $tblItem->getItem(array('page' => 'privacy-policy'), array('task' => 'base-alias'));
    	$this->view->term = $tblItem->getItem(array('page' => 'terms-of-use'), array('task' => 'base-alias'));
    	
        if(!isset($this->_arrParam['call'])){
    	   $this->_helper->layout->disableLayout();	
        }
    }
	
	public function addEventsAction(){
    	$this->_helper->layout->disableLayout();
        $tblEvent = new Zendvn_Model_Table_Event();

        if($this->_request->isPost()){
            $validator = new Default_Form_ValidateEvent($this->_arrParam);
            if($validator->isVaild()){
                $arrError = $validator->getMessageErrors();
                $this->view->Item 	= $validator->getData();
                $response = array('type' => 'error','msg'=>$arrError);
            }else{

                $this->_helper->flashMessenger->addMessage(array('success'=>'The updated was successful'));
                $data =  $validator->getData();
				$date = empty($data['date'])?"now":$data['date'];
                $data['date'] = date("Y-m-d", strtotime($date));
                $data['status'] = 'active';

                try{
                    $returnID = Zendvn_Wordpress::getInstance()->createPost($data);
                    $response = array('type' => 'success');
                }catch (Exception $e){
                    $response = array('type' => 'error','msg'=>$e->getMessage());
                }
                $data['wp_id'] = $returnID;
                $tblEvent->saveItem($data,array('task'=>'admin-add'));
            }
            echo json_encode($response);
            $this->_helper->viewRenderer->setNoRender();
        }

    }

    public function eventFinishAction(){

    	$this->_helper->layout->disableLayout();
    }

    public function donateAction(){

    	$this->_helper->layout->disableLayout();
    }

    public function searchItemAction(){
        $tblCat = new Zendvn_Model_Table_NewsCategory();

        $this->view->slbCategory = $tblCat->itemInSelect($this->_arrParam, array('task'=>'item-add'));

        $this->_helper->layout->disableLayout();
    }

    public function testAction(){
        $mail = new Zendvn_Mail();
                $mail->sendRegister($this->_arrParam); 
        die('test');
    }

    public function checkNameAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        $tblEvent = new Zendvn_Model_Table_Event();
        echo $tblEvent->getItem($this->_arrParam,array('task'=>'user'));
    }

    public function checkoutAction(){
        $value = $this->_arrParam['value'];

        $paypal = new Zendvn_Paypal();
        $result = $paypal->setExpressCheckout($value);

        $this->redirect($result);
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

    }
}